package system.student.login.impl;

import system.student.entity.updatepassword.UpdatePasswordRequest;
import system.student.entity.updatepassword.UpdatePasswordState;
import system.student.entity.user.Teacher;
import system.student.entity.user.UserInfo;
import system.student.login.UpdatePasswordStrategy;
import system.student.mapper.TeacherMapper;
import system.student.mapper.UserMapper;
import system.student.util.MapperUtil;
import system.student.util.SmsCodeUtil;

import java.util.Scanner;

public class UsePhoneUpdate implements UpdatePasswordStrategy {
    private static final UserMapper userMapper = MapperUtil.getMapper(UserMapper.class);

    @Override
    public void updatePassword(UpdatePasswordRequest request) {
        String phone = request.getPhone();
        UserInfo user = userMapper.selectByUserId(request.getUserId());
        if (!user.getPhone().equals(phone)) {
            System.out.println("手机号错误");
            return;
        }
        String smsCode = SmsCodeUtil.createSmsCode();
        System.err.println("验证码为: " + smsCode);
        System.out.println("请输入验证码");
        Scanner scanner = new Scanner(System.in);
        String code = scanner.nextLine();
        if (code.equals(smsCode)) {
            System.out.println("请输入新密码");
            String password = scanner.nextLine();
            userMapper.updatePasswordByUserId(request.getUserId(), password);
            System.out.println("修改成功");
        }else {
            System.out.println("验证码错误");
        }
    }

    @Override
    public UpdatePasswordState getState() {
        return UpdatePasswordState.Phone;
    }
}
